package org.quipu.domain;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "CURRENCY", uniqueConstraints = @UniqueConstraint(columnNames = {"ID_BOOK", "TITLE"}))
@NamedQueries({
    @NamedQuery(name = "Currency.findAll", query = "SELECT c FROM Currency c"),
    @NamedQuery(name = "Currency.findById", query = "SELECT c FROM Currency c WHERE c.id=:id"),
    @NamedQuery(name = "Currency.findByTitleForBook", query = "SELECT c FROM Currency c WHERE c.title=:title AND c.book.id=:bookId")
})
public class Currency extends UniqueEntity implements Serializable {

    @Column(name = "TITLE", length = 32, nullable = false)
    private String title;

    @JoinColumn(name = "ID_BOOK", referencedColumnName = "ID", nullable = false)
    @ManyToOne
    private Book book;

    @JoinColumn(name = "ID_CURRENCY_DIC", referencedColumnName = "ID", nullable = true)
    @ManyToOne
    private CurrencyDic dicCurrency;

    @OneToMany(mappedBy = "currency", cascade = CascadeType.REMOVE)
    private Collection<Transaction> transactions;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public Collection<Transaction> getTransactions() {
        return transactions;
    }

    public void setTransactions(Collection<Transaction> transactions) {
        this.transactions = transactions;
    }

    public CurrencyDic getDicCurrency() {
        return dicCurrency;
    }

    public void setDicCurrency(CurrencyDic dicCurrency) {
        this.dicCurrency = dicCurrency;
    }

    @Override
    public String toString() {
        return "{ id=" + getId() + "; title=" + getTitle() + "; }";
    }

    @Override
    public boolean equals(Object cur) {
        return cur instanceof Currency && getId().equals(((Currency)cur).getId());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (getId() != null ? getId().hashCode() : 0);
        return hash;
    }
}
